package com.hjwoo.app.ui;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;

import com.hjwoo.app.utils.LoanLog;

import java.util.Hashtable;

/**
 * Created by yangchuan on 2016/6/7.
 */
public class LineWrapLinear extends LinearLayout
{
    private int mLeft, mRight, mTop, mBottom;
    private Hashtable map = new Hashtable();

    public LineWrapLinear(Context context)
    {
        super(context);
    }

    public LineWrapLinear(Context context, AttributeSet attrs)
    {
        super(context, attrs);
    }

    public LineWrapLinear(Context context, AttributeSet attrs, int defStyleAttr)
    {
        super(context, attrs, defStyleAttr);
    }

    public int getPosition(int IndexInRow, int childIndex)
    {
        if (IndexInRow > 0)
        {
            return getPosition(IndexInRow - 1, childIndex - 1)
                   + getChildAt(childIndex - 1).getMeasuredWidth() + 8;
        }
        return getPaddingLeft();
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b)
    {
        int count = getChildCount();
        for (int i = 0; i < count; i++)
        {
            View     child = getChildAt(i);
            Position pos   = (Position) map.get(child);
            if (pos != null)
            {
                child.layout(pos.left, pos.top, pos.right, pos.bottom);
            }
            else
            {
               LoanLog.e("MyLayout", "error");
            }
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
        int mWidth = MeasureSpec.getSize(widthMeasureSpec);
        int mCount = getChildCount();
        int mX     = 0;
        int mY     = 0;
        mLeft = 0;
        mRight = 0;
        mTop = 0;
        mBottom = 0;
        int  j        = 0;
        View lastview = null;
        for (int i = 0; i < mCount; i++)
        {
            final View child = getChildAt(i);

            child.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
            int childw = child.getMeasuredWidth();
            int childh = child.getMeasuredHeight();
            mX += childw;

            Position position = new Position();
            mLeft = getPosition(i - j, i);
            mRight = mLeft + child.getMeasuredWidth();
            if (mX >= mWidth)
            {
                mX = childw;
                mY += childh;
                j = i;
                mLeft = 0;
//                mRight = mLeft;
                mRight = mLeft + child.getMeasuredWidth();
//                mTop = mY ;
                mTop = mY + 5;
            }
            mBottom = mTop + child.getMeasuredHeight();
            mY = mTop;
            position.left = mLeft;
            position.top = mTop + 3;
            position.right = mRight;
            position.bottom = mBottom;
            map.put(child, position);
        }
        setMeasuredDimension(mWidth, mBottom);
    }

    @Override
    protected void dispatchDraw(Canvas canvas)
    {
        // 获取布局控件宽高
        int width  = getWidth();
        int height = getHeight();
        // 创建画笔
        Paint mPaint = new Paint();
        // 设置画笔的各个属性
        mPaint.setColor(Color.TRANSPARENT);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(0);
        mPaint.setAntiAlias(true);
        // 创建矩形框
        Rect mRect = new Rect(0, 0, width, height);
        // 绘制边框
        canvas.drawRect(mRect, mPaint);
        // 最后必须调用父类的方法
        super.dispatchDraw(canvas);
    }

    private class Position
    {
        int left, top, right, bottom;
    }
}
 
